package com.tlgen.modules.system.repository;

import com.tlgen.modules.system.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;

/**
 * @author: tlgen
 * @Discription: 用户持久层
 */
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    /**
     * 根据用户查询
     * @param username
     * @return
     */
    User findByUsername(String username);

    /**
     * 根据邮箱查询
     * @param email
     * @return
     */
    User findByEmail(String email);

    /**
     *修改密码
     * @param username
     * @param pass
     * @param lastPasswordResetTime
     */
    @Query(value = "update user set password = ?2, last_password_reset_time= ?3 where username = ?1", nativeQuery = true)
    void updatePass(String username, String pass, Date lastPasswordResetTime);

    /**
     *修改邮箱
     * @param username
     * @param email
     */
    @Modifying
    @Query(value = "update user set email = ?2 where username = ?1", nativeQuery = true)
    void updateEmail(String username, String email);

}
